package screens;

import log.Logger;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import strings.WebcampusResource;
import util.LookAndFeel;
import beans.Course;
import beans.Person;
import customfields.OptionField;
import customfields.WhiteLabelField;


public class GroupScreen extends TransitionScreen implements WebcampusResource{

	// RESOURCES
	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	private Course course;;

	public GroupScreen(Course c, int transitionType){
		super(transitionType);
		
		Logger.log("creating group screen");

		this.course=c;

		VerticalFieldManager vfm;
		if (this.course.getGroup().getMembers().size()<=2){
		// Main vertical field manager
		vfm = new VerticalFieldManager() {
			protected void sublayout( int maxWidth, int maxHeight )
			{
				//set width
				int displayWidth = Display.getWidth();
				//set height
				int displayHeight = Display.getHeight();
				super.sublayout( displayWidth, displayHeight);
				setExtent( displayWidth, displayHeight);
				System.out.println(displayHeight + "" +displayWidth);
			}
		};
		} else {
			vfm = new VerticalFieldManager();
		}
		
		Bitmap background_bitmap = Bitmap.getBitmapResource(LookAndFeel.background_img);
		Bitmap background_bitmap_resized = new Bitmap(Display.getWidth(), background_bitmap.getHeight());
		background_bitmap.scaleInto(background_bitmap_resized, Bitmap.FILTER_LANCZOS | Bitmap.SCALE_TO_FILL);
		Background background = BackgroundFactory.createBitmapBackground(background_bitmap_resized);
		vfm.setBackground(background);

		// Header
		Bitmap header_img = Bitmap.getBitmapResource(LookAndFeel.header_img);
		Bitmap header_img_resized = new Bitmap(Display.getWidth(), (int) (Display.getHeight()*0.25));
		header_img.scaleInto(header_img_resized, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
		BitmapField header = new BitmapField(header_img_resized,Field.FIELD_HCENTER);
		vfm.add(header);

		// Separator
		vfm.add(new SeparatorField());
		vfm.add(new LabelField(""));

		WhiteLabelField groupName = new WhiteLabelField(this.course.getGroup().getName(),Field.FIELD_HCENTER);
		vfm.add(groupName);

		vfm.add(new WhiteLabelField(""));
		for (int i=0; i<this.course.getGroup().getMembers().size(); i++){
			vfm.add(new WhiteLabelField(((Person) this.course.getGroup().getMembers().elementAt(i)).getName(), Field.FIELD_HCENTER));
		}
		vfm.add(new LabelField(""));

		OptionField messages = new OptionField(resources.getString(GROUP_MESSAGES_LABEL), Field.FIELD_HCENTER| Field.FOCUSABLE);
		messages.setBackground(BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color,
				LookAndFeel.button_upper_color,  LookAndFeel.button_lower_color,  LookAndFeel.button_lower_color));
		messages.setChangeListener(messagesListener);
		vfm.add(messages);
		OptionField files = new OptionField(resources.getString(GROUP_FILES_LABEL), Field.FIELD_HCENTER| Field.FOCUSABLE);
		files.setBackground(BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color,
				LookAndFeel.button_upper_color,  LookAndFeel.button_lower_color,  LookAndFeel.button_lower_color));
		files.setChangeListener(filesListener);
		vfm.add(files);

		add(vfm);

	}

	private FieldChangeListener messagesListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			Controller.getInstance().openGroupMessagesScreen(course);
		}
	};


	private FieldChangeListener filesListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			if (course.getGroup().getFiles().isEmpty() || course.getGroup().getFiles()==null){
				Dialog.inform(resources.getString(ALERT_NOGROUPFILES));
			}else {
				Controller.getInstance().openGroupFilesScreen(course.getGroup());
			}

		}
	};
	
}

